package com.taotao.manage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;

@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {

	@Autowired
	ContentCategoryService contentCategoryService;
	
	/**
	 * 内容分类管理页面加载: 查询所有的ContentCategory
	 * 异步
	 * 参数: 
	 * 返回结果: list<ContentCategory>
	 * 
	 * @return
	 */
	
	@GetMapping
	public ResponseEntity<List<ContentCategory>> queryContentCategoryByParentId(
			@RequestParam(value="id", defaultValue="0") Long parentId){
		try {
			ContentCategory record = new ContentCategory();
			record.setParentId(parentId);
			record.setStatus(1);
			List<ContentCategory> list = contentCategoryService.queryByRecord(record);
			return ResponseEntity.status(HttpStatus.OK).body(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 新增节点: 1)保存当前节点, 2)判断当前节点的父节点的isParent属性是否为true,不是的话更改为true
	 * 异步
	 * 参数: parentId name
	 * 返回值: 新增的该ContentCategory对象
	 * @return
	 */
	@PostMapping
	public ResponseEntity<ContentCategory> addContentCategory(
			@RequestParam("parentId") Long parentId,
			@RequestParam("name") String name){
		try {
			ContentCategory category = contentCategoryService.addContentCategory(parentId, name);
			return ResponseEntity.status(HttpStatus.OK).body(category);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 修改名称
	 * 异步
	 * 参数: id name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateContentCategory(@RequestParam("id")Long id,@RequestParam("name") String name){
		try {
			ContentCategory record = new ContentCategory();
			record.setId(id);
			record.setName(name);
			contentCategoryService.update(record);
			return ResponseEntity.status(HttpStatus.OK).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	/**
	 * 删除节点, 1)递归删除其全部子节点, 2)检查其父节点是否还有其他子节点, 如果没有更新父节点的isParent属性为false
	 * 异步
	 * 参数: parentId id 
	 * 返回结果: 无
	 * @return
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteContentCategory(@RequestParam("id")Long id,@RequestParam("parentId") Long parentId){
		try {
			if(contentCategoryService.deleteContentCategory(id, parentId)){
				return ResponseEntity.status(HttpStatus.OK).build();
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	
}







